from django.shortcuts import render
from rest_framework.generics import ListAPIView
from rest_framework.filters import OrderingFilter
from drf_haystack.viewsets import HaystackViewSet


from .serializers import SKUSerializer, SKUIndexSerializer
from .models import SKU

# Create your views here.

# # GET /categories/(?P<category_id>\d+)/skus?page=xxx&page_size=xxx&ordering=xxx
class SKUListView(ListAPIView):
    '''
        列表视图
    '''
    # 指定排序的后端面:指定它是为了下面的ordering_fields
    filter_backends = [OrderingFilter]
    # 指定排序字段
    ordering_fields = ('create_time', 'price', 'sales')
    # 　获取序列化器d
    serializer_class = SKUSerializer
    # 查询集
    def get_queryset(self):
        category_id = self.kwargs.get("category_id")
        return SKU.objects.filter(category__id=category_id, is_launched=True)


class SKUSearchViewSet(HaystackViewSet):
    """
    SKU搜索
    """
    index_models = [SKU]

    serializer_class = SKUIndexSerializer